<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>明暗主题动态切换动画</title>
    <style>
        /* 基础样式 */
        body {
            margin: 0;
            padding: 0;
            font-family: 'Arial', sans-serif;
            transition: background-color 0.3s ease, color 0.3s ease;
            /* 默认亮色主题 */
            background-color: #f5f5f5;
            color: #333;
        }

        .container {
            max-width: 800px;
            margin: 0 auto;
            padding: 20px;
        }

        h1 {
            text-align: center;
            margin-bottom: 30px;
        }

        .content {
            background-color: white;
            padding: 20px;
            border-radius: 8px;
            box-shadow: 0 2px 10px rgba(0, 0, 0, 0.1);
            margin-bottom: 20px;
        }

        .theme-toggle {
            display: block;
            margin: 0 auto;
            padding: 10px 20px;
            background-color: #4a90e2;
            color: white;
            border: none;
            border-radius: 4px;
            cursor: pointer;
            font-size: 16px;
            transition: background-color 0.3s;
        }

        .theme-toggle:hover {
            background-color: #3a7bc8;
        }

        /* 暗色主题样式 */
        body.dark-theme {
            background-color: #121212;
            color: #e0e0e0;
        }

        body.dark-theme .content {
            background-color: #1e1e1e;
            box-shadow: 0 2px 10px rgba(0, 0, 0, 0.3);
        }

        body.dark-theme .theme-toggle {
            background-color: #bb86fc;
            color: #121212;
        }

        body.dark-theme .theme-toggle:hover {
            background-color: #9d6ad9;
        }

        /* 主题切换动画容器 */
        .theme-transition-container {
            position: fixed;
            top: 0;
            left: 0;
            width: 100%;
            height: 100%;
            pointer-events: none;
            z-index: 1000;
        }

        /* 主题切换层 */
        .theme-transition-layer {
            position: absolute;
            top: 0;
            left: 0;
            width: 100%;
            height: 100%;
            opacity: 0;
            /* 默认使用亮色主题背景 */
            background-color: #f5f5f5;
        }

        /* 暗色主题切换层 */
        .theme-transition-layer.dark {
            background-color: #121212;
        }

        /* 定义主题切换动画 */
        @keyframes wipe-out-left {
            from {
                clip-path: inset(0 0 0 0);
            }
            to {
                clip-path: inset(0 100% 0 0);
            }
        }

        @keyframes wipe-in-right {
            from {
                clip-path: inset(0 100% 0 0);
            }
            to {
                clip-path: inset(0 0 0 0);
            }
        }

        /* 应用动画到主题切换层 */
        .theme-transition-layer.wipe-out-left {
            animation: 2.5s cubic-bezier(.25, 1, .30, 1) wipe-out-left both;
        }

        .theme-transition-layer.wipe-in-right {
            animation: 2.5s cubic-bezier(.25, 1, .30, 1) wipe-in-right both;
        }
    </style>
</head>
<body>
<div class="container">
    <h1>明暗主题切换演示</h1>

    <div class="content">
        <p>这是一个演示明暗主题动态切换效果的页面。点击下方的按钮可以体验平滑的主题切换动画。</p>
        <p>动画使用了CSS的<code>clip-path</code>属性，通过改变裁剪区域来实现从左到右的擦除效果。</p>
        <p>亮色主题适合白天使用，暗色主题适合夜间使用，可以减少眼睛疲劳。</p>
    </div>

    <button id="themeToggle" class="theme-toggle">切换到暗色主题</button>
</div>

<!-- 主题切换动画容器 -->
<div id="themeTransitionContainer" class="theme-transition-container"></div>

<script>
    // 获取DOM元素
    const themeToggle = document.getElementById('themeToggle');
    const themeTransitionContainer = document.getElementById('themeTransitionContainer');
    const body = document.body;

    // 当前主题状态
    let isDarkTheme = false;

    // 切换主题函数
    function toggleTheme() {
        // 创建新的主题切换层
        const transitionLayer = document.createElement('div');

        // 根据当前主题设置切换层的样式
        if (isDarkTheme) {
            // 从暗色切换到亮色
            transitionLayer.className = 'theme-transition-layer';
            transitionLayer.style.opacity = '1';

            // 添加离开动画
            transitionLayer.classList.add('wipe-out-left');

            // 动画结束后移除切换层并更新主题
            transitionLayer.addEventListener('animationend', () => {
                body.classList.remove('dark-theme');
                themeTransitionContainer.removeChild(transitionLayer);
                themeToggle.textContent = '切换到暗色主题';
            }, { once: true });

        } else {
            // 从亮色切换到暗色
            transitionLayer.className = 'theme-transition-layer dark';
            transitionLayer.style.opacity = '1';

            // 添加进入动画
            transitionLayer.classList.add('wipe-in-right');

            // 动画结束后更新主题并移除切换层
            transitionLayer.addEventListener('animationend', () => {
                body.classList.add('dark-theme');
                themeTransitionContainer.removeChild(transitionLayer);
                themeToggle.textContent = '切换到亮色主题';
            }, { once: true });
        }

        // 将切换层添加到容器中
        themeTransitionContainer.appendChild(transitionLayer);

        // 切换主题状态
        isDarkTheme = !isDarkTheme;
    }

    // 添加按钮点击事件监听器
    themeToggle.addEventListener('click', toggleTheme);
</script>
</body>
</html>